package com.fpmsa.officeweb.controller;


import com.alibaba.fastjson.JSON;
import com.fpmsa.officeweb.constant.OWConstant;
import com.fpmsa.officeweb.model.Member;
import com.fpmsa.officeweb.model.News;
import com.fpmsa.officeweb.service.IMemberService;
import com.fpmsa.officeweb.service.INewsService;
import com.fpmsa.officeweb.service.OfficeWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/member")
public class WebMemberController {

    private String PREFIX = "/templates/member/";

    // “个人”、“单位”
    private Integer PERSONAL = 0;
    private Integer UNIT = 1;

    // "理事"、"监事"
    private Integer COUNCIL = 2;
    private Integer SUPERVISOR = 3;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private OfficeWebService officeWebService;

    @Autowired
    private INewsService newsService;


    /**
     * 跳转到“会员名录”页面
     */
    @RequestMapping("/list")
    public String memberLsit(Model model){

        // 获取会员（包括个人和单位）列表和总个数
        List<Member> memberList = memberService.getMembers(10);
        Integer countMember = memberService.getCountOfMember();
        model.addAttribute("memberList", JSON.toJSONString(memberList).replace("\"","\'"));
        model.addAttribute("countMember",countMember);//页面暂未使用

        if (countMember > 10){
            model.addAttribute("isShowMore",1);//表示页面显示“点击查看更多”按钮
        }else{
            model.addAttribute("isShowMore",0);//不显示
        }

        // 用来判断页面上将左边选中的菜单文字设置成蓝色（class=on）
        model.addAttribute("flagname",OWConstant.OW_MEMBER_LIST);

        // 设置页面keyword、description
        model.addAttribute("title_suffix",OWConstant.OW_TITLE_SUFFIX);
        model.addAttribute("keywords",OWConstant.OW_KEYWORDS);
        model.addAttribute("description",OWConstant.OW_DESCRIPTION);

        return PREFIX + "member_list.html";
    }


    /**
     * 获取会员列表(“点击查看更多”功能),由10条逐步增加改为一次性全部加载
     */
    @RequestMapping("/getMoreMembers")
    @ResponseBody
    public String getMoreMembers(){
        Integer countMember = memberService.getCountOfMember();
        List<Member> memberList = memberService.getMembers(countMember);
        return JSON.toJSONString(memberList);
    }


    /**
     * 跳转到“理监事名录”页面
     */
    @RequestMapping("/committeeList")
    public String committeeList(Model model){

        // 获取理事列表和总个数
        List<Member> councilList = memberService.getComittees(COUNCIL,10);
        Integer countCouncil = memberService.getCountOfType(COUNCIL);
        model.addAttribute("councilList", JSON.toJSONString(councilList).replace("\"","\'"));
        model.addAttribute("countCouncil",countCouncil);//页面暂未使用
        if (countCouncil > 10){
            model.addAttribute("isShowMore",1);//表示页面显示“点击查看更多”按钮
        }else{
            model.addAttribute("isShowMore",0);//不显示
        }

        // 获取监事列表和总个数
        List<Member> supervisorList = memberService.getComittees(SUPERVISOR,10);
        Integer countSupervisor = memberService.getCountOfType(SUPERVISOR);
        model.addAttribute("supervisorList", JSON.toJSONString(supervisorList).replace("\"","\'"));
        model.addAttribute("countSupervisor",countSupervisor);//页面暂未使用
        if (countSupervisor > 10){
            model.addAttribute("isShowMore1",1);//表示页面显示“点击查看更多”按钮
        }else{
            model.addAttribute("isShowMore1",0);//不显示
        }

        // 用来判断页面上将左边选中的菜单文字设置成蓝色（class=on）
        model.addAttribute("flagname",OWConstant.OW_COMMITTEE_LIST);

        // 设置页面keyword、description
        model.addAttribute("title_suffix",OWConstant.OW_TITLE_SUFFIX);
        model.addAttribute("keywords",OWConstant.OW_KEYWORDS);
        model.addAttribute("description",OWConstant.OW_DESCRIPTION);

        return PREFIX + "committee_list.html";
    }

    /**
     * 获取个人会员列表
     */
    @RequestMapping("/getCouncils")
    @ResponseBody
    public String getCouncils(){
        List<Member> councilList = memberService.getComittees(COUNCIL,null);
        return JSON.toJSONString(councilList);
    }


    /**
     * 获取单位会员列表
     */
    @RequestMapping("/getSupervisors")
    @ResponseBody
    public String getSupervisors(){
        List<Member> supervisorList = memberService.getComittees(SUPERVISOR,null);
        return JSON.toJSONString(supervisorList);
    }


    /**
     * 跳转到“会员中心”--“会员风采”页面
     */
    @RequestMapping("/memberphoto")
    public String memberPhoto(Model model){

        // 获取新闻列表（类别为“会员风采”）
        List<News> newsMapList = officeWebService.getNumNewsByCategory(OWConstant.OW_MEMBER_DEMEANOUR,null,false);
        // 转换newsList中creat_time字段的格式为yyyy/MM/dd
        List<Map<String,Object>> memberPhotoMapList = officeWebService.formatCreateTime(newsMapList,"yyyy-MM-dd");
        model.addAttribute("memberPhotoMapList", memberPhotoMapList);

        // 用来判断页面上将左边选中的菜单文字设置成蓝色（class=on）
        model.addAttribute("flagname",OWConstant.OW_MEMBER_DEMEANOUR);

        // 设置页面keyword、description
        model.addAttribute("title_suffix",OWConstant.OW_TITLE_SUFFIX);
        model.addAttribute("keywords",OWConstant.OW_KEYWORDS);
        model.addAttribute("description",OWConstant.OW_DESCRIPTION);

        return PREFIX + "member_photo.html";
    }

    /**
     * 跳转到“会员中心”--“加入会员”页面
     */
    @RequestMapping("/memberjoin")
    public String memberjoin(Model model){

        // 用来判断页面上将左边选中的菜单文字设置成蓝色（class=on）
        model.addAttribute("flagname",OWConstant.OW_MEMBER_DEMEANOUR);

        // 设置页面keyword、description
        model.addAttribute("title_suffix",OWConstant.OW_TITLE_SUFFIX);
        model.addAttribute("keywords",OWConstant.OW_KEYWORDS);
        model.addAttribute("description",OWConstant.OW_DESCRIPTION);

        return PREFIX + "member_join.html";
    }
}
